<?php

require_once("../HT_Client/HT_Client.class.php");
require_once("../functions.inc.php");
require_once("../database.inc.php");
require_once("./hoccleanup.inc.php");

$dbh = connect_db();
$client = new HT_Client();

$client->Login("username",'security_code',"Maptrick 2.0 daily DB refresh @ 11:00");

$world = $client->GetWorldInfo();
if (ereg("err", $world)) die ("failed to get worldinfo - HT down?\n");

foreach ($world as $country) {
	if ($country['LeagueID'] > 0 ) {
		$tmp = mysql_query("SELECT NumberOfOnline FROM leagues_daily WHERE LeagueID = {$country['LeagueID']}", $dbh);
		if ($tmp) {
			echo ".";
			$training = $client->GetTrainingStats($country['LeagueID']);
			$inserttraining = ("REPLACE INTO training () VALUES (" .
			$country['LeagueID'] . "," .
			$training[0] . "," .
			$training[1] . "," .
			$training[2] . "," .
			$training[3] . "," .
			$training[4] . "," .
			$training[5] . "," .
			$training[6] . "," .
			$training[7] . "," .
			$training[8] . "," .
			$training[9] . "," .
			$training[10] . "," .
			$training[11] . "," .
			$training[12] . ")");
			
			
			if (!mysql_query("$inserttraining", $dbh)) {
				die (mysql_error() . "<br/>\n");
			}
			
			
			$last_number_online = mysql_result($tmp, 0);

			$query = ("REPLACE INTO aw () values (NOW(), " . 
			mysql_real_escape_string($country['LeagueID']) . "," .
			mysql_real_escape_string($country['ActiveUsers']) . "," .
			mysql_real_escape_string($country['WaitingUsers']) . ")");

			if (!mysql_query($query, $dbh)) {
				echo mysql_error() . "<br/>\n";	
			}


			$insert = ("REPLACE INTO leagues_daily () VALUES ('" .
			mysql_real_escape_string($country['LeagueID']) . "','" .
			mysql_real_escape_string($country['LeagueName']) . "','" .
			mysql_real_escape_string($country['Season']) . "','" .
			mysql_real_escape_string($country['MatchRound']) . "','" .
			mysql_real_escape_string($country['ShortName']) . "','" .
			mysql_real_escape_string($country['Continent']) . "','" .
			mysql_real_escape_string($country['ZoneName']) . "','" .
			mysql_real_escape_string($country['EnglishName']) . "','" .
			mysql_real_escape_string($country['CurrencyName']) . "','" .
			mysql_real_escape_string(str_replace(",", ".", $country['CurrencyRate'])) . "','" .
			mysql_real_escape_string($country['CupID']) . "','" .
			mysql_real_escape_string($country['CupName']) . "','" .
			mysql_real_escape_string($country['ActiveUsers']) . "','" .
			mysql_real_escape_string($country['WaitingUsers']) . "','" .
			mysql_real_escape_string($country['TrainingDate']) . "','" .
			mysql_real_escape_string($country['EconomyDate']) . "','" .
			mysql_real_escape_string($country['CupMatchDate']) . "','" .
			mysql_real_escape_string($country['SeriesMatchDate']) . "','" .
			mysql_real_escape_string($country['NumberOfLevels']) . "','" . 
			levels2teams($country['NumberOfLevels']) . "','$last_number_online')");
		
			if (!mysql_query("$insert", $dbh)) {
				die (mysql_error() . "<br/>\n");
			}
		} else {
			die(mysql_error() . "<br/>\n");
		}
	}
}
echo "\n";

$languages = $client->GetLanguageInfo();
if (ereg("err", $world)) die ("failed to get languageInfo - HT down?\n");

foreach ($languages as $language) {
	echo "-";
	if (!mysql_query("REPLACE INTO languages VALUES({$language['LanguageID']}, '{$language['LanguageName']}')", $dbh)) die (mysql_error());
}

$query = mysql_query("SELECT SUM(activeusers) au, SUM(waitingusers) wu, SUM(numberofteams) nt, SUM(numberofonline) no FROM leagues_daily WHERE LeagueID = 2 OR LeagueID = 26 OR LeagueID = 61 OR LeagueID = 93", $dbh);

while ($line = mysql_fetch_array($query)) {
	echo ";";
	mysql_query("REPLACE INTO leagues_daily (LeagueID, LeagueName, ShortName, Continent, ZoneName, Englishname, ActiveUsers, WaitingUsers, NumberOfTeams, NumberOfOnline) VALUES('255', 'United Kingdom', 'UK', 'Europe', 'Western Europe', 'United Kingdom', '{$line['au']}', '{$line['wu']}', '{$line['nt']}', '{$line['no']}')", $dbh);
}

$ranges = array(
	array(1,27),
	array(48,293),
	array(295,602),
	array(604,620),
	array(622,649),
	array(651,849),
	array(861,867),
	array(879,1236),
	array(1249,1366),
	array(1369,1460),
	array(1510,1554),
	array(1566,1620),
	array(1628,1860),
	array(1874,1892),
	array(1894,2170),
	array(2172,2201),
	array(2204,2204),
	array(2210,2211),
	array(2213,2377),
	array(2379,2445)
);


/*

$missing_ranges = array(
	array(28,47),
	array(294,294),
	array(603,603),
	array(621,621),
	array(650,650),
	array(850,860),
	array(868,878),
	array(1237,1248),
	array(1367,1368),
	array(1461,1509),
	array(1555,1565),
	array(1621,1627),
	array(1861,1873),
	array(1893,1893),
	array(2171,2171),
	array(2241,2246),
	array(2378,2378)
);

*/

foreach (array_keys($ranges) as $range) {
	echo ",";
	getrange($ranges[$range][0], $ranges[$range][1], $client, $dbh);
}

if (!$client->Logout()) echo "Logout failed!";
mysql_query("DELETE FROM `regions` WHERE RegionID = 0");
mysql_query("DELETE FROM `leagues_daily` WHERE LeagueID = 0");

foreach ($world as $country) {
	echo ":";
	if ($country['LeagueID'] > 0 ) {
		$tmp = mysql_query("SELECT sum(NumberOfOnline) FROM regions WHERE regions.LeagueID ={$country['LeagueID']}", $dbh);
		$numberofonline = mysql_result($tmp, 0);

		if (!mysql_query("UPDATE leagues_daily SET NumberOfOnline=$numberofonline WHERE LeagueID = {$country['LeagueID']}", $dbh)) {
			die (mysql_error() . "<br/>\n");
		}
	}
}
echo "\n";

mysql_close($dbh);

$output = array();
exec('/www/maptrick/htdocs/convenience/updaterrds.pl', $output);
if (sizeof($output) > 0 ) { var_dump($output); }

function getregion($regionid, $client, $dbh) {
	$values = $client->GetRegionInfo($regionid);
	#echo "$values[FetchedDate] $regionid<br/>\n";
	if ($values['LeagueID'] == 0) { echo "$regionid doesn't exist!\n"; } 
	mysql_query(
		"REPLACE INTO regions ()
		VALUES (
			'$values[RegionID]',
			'" . mysql_real_escape_string($values['RegionName']) . "',
			'$values[LeagueID]',
			'$values[WeatherID]',
			'$values[TomorrowWeatherID]',
			'$values[NumberOfUsers]',
			'$values[NumberOfOnline]',
			NOW()
		)", $dbh
	) or die (mysql_error());
}


function getrange($from, $to, $client, $dbh) {
	for($i = $from; $i <= $to; $i++) {
		echo "+";
		getregion($i, $client, $dbh);
	}
}

?>
